package com.vk.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vk.project.common.ErrorCode;
import com.vk.project.exception.BusinessException;
import com.vk.project.mapper.UserInterfaceInfoMapper;
import com.vk.project.model.vo.ChartVO;
import com.vk.project.service.UserInterfaceInfoService;
import com.vk.vkapicommon.model.entity.UserInterfaceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : UserInterfaceInfoServiceImpl
 * @Description : 用户接口信息服务实现类
 * @Author : LYQ
 * @Date: 2023-11-30 18:33:58
 */
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
        implements UserInterfaceInfoService {

    @Autowired
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    /**
     * 调用接口时用户和接口信息相关操作
     *
     * @param userInterfaceInfo 用户和接口信息对象
     * @param add               是否添加
     */
    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {
        // 如果用户接口信息对象为空，则抛出参数错误异常
        if (userInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 创建时，所有参数必须非空
        if (add) {
            if (userInterfaceInfo.getInterfaceInfoId() <= 0 || userInterfaceInfo.getUserId() <= 0) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口或用户不存在");
            }
        }
        if (userInterfaceInfo.getLeftNum() < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "剩余次数不能小于 0");
        }
    }

    /**
     * 调用接口统计
     *
     * @param interfaceInfoId 接口id
     * @param userId          用户id
     * @return 返回是否计数成功
     */
    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        // 判断
        if (interfaceInfoId <= 0 || userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interfaceInfoId", interfaceInfoId);
        updateWrapper.eq("userId", userId);
        updateWrapper.gt("leftNum", 0);
        updateWrapper.setSql("leftNum = leftNum - 1, calledNum = calledNum + 1");

        return this.update(updateWrapper);
    }

    /**
     * 根据接口Id获取与该接口相关的用户接口信息
     *
     * @param interfaceId 接口Id
     * @return 返回用户接口信息列表
     */
    @Override
    public List<UserInterfaceInfo> getUserInterfaceInfoListById(long interfaceId) {

        LambdaQueryWrapper<UserInterfaceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInterfaceInfo::getInterfaceInfoId, interfaceId);

        List<UserInterfaceInfo> userInterfaceInfos = userInterfaceInfoMapper.selectList(queryWrapper);

        return userInterfaceInfos;
    }

    /**
     * 根据接口Id删除与该接口相关的用户接口信息
     *
     * @param interfaceId 接口Id
     * @return 返回是否删除成功
     */
    @Override
    public boolean deleteInterfaceInfoByInterfacId(long interfaceId) {
        boolean deleted = userInterfaceInfoMapper.deleteInterfaceInfoByInterfacId(interfaceId);
        return deleted;
    }

    /**
     * 根据用户Id删除与该用户相关的用户接口信息
     *
     * @param id 用户Id
     * @return 返回删除结果
     */
    @Override
    public boolean removeUserInterfaceInfoByUid(Long id) {
        LambdaQueryWrapper<UserInterfaceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInterfaceInfo::getUserId, id);
        int deleted = userInterfaceInfoMapper.delete(queryWrapper);
        return deleted > 0 ? true : false;
    }

    /**
     * 根据用户Id和接口Id获取剩余调用次数
     *
     * @param userId      用户Id
     * @param interfaceId 接口Id
     * @return 返回剩余次数
     */
    @Override
    public int getleftNumByUserIdAndInterfaceId(long userId, long interfaceId) {
        LambdaQueryWrapper<UserInterfaceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInterfaceInfo::getUserId, userId)
                .eq(UserInterfaceInfo::getInterfaceInfoId, interfaceId);
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoMapper.selectOne(queryWrapper);
        return userInterfaceInfo.getLeftNum();
    }

    /**
     * 给用户再增加50次接口调用次数
     *
     * @param userId      用户Id
     * @param interfaceId 接口Id
     * @return
     */
    @Override
    public boolean addBy50Times(long userId, long interfaceId) {
        // 判断
        if (interfaceId <= 0 || userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interfaceInfoId", interfaceId);
        updateWrapper.eq("userId", userId);
        updateWrapper.eq("leftNum", 0);
        updateWrapper.setSql("leftNum = 50");

        return this.update(updateWrapper);
    }

    /**
     * 获取已发放次数
     *
     * @return 返回已发放次数
     */
    @Override
    public int getIssuedTimes() {
        return userInterfaceInfoMapper.getIssuedTimes();
    }

    /**
     * 获取累计调用
     *
     * @return 返回累计调用
     */
    @Override
    public int getAccumulatedCalls() {
        return userInterfaceInfoMapper.getAccumulatedCalls();
    }

    /**
     * 获取用户调用次数
     *
     * @return 返回数据
     */
    @Override
    public List<ChartVO> getNumberOfUserCalls() {
        return userInterfaceInfoMapper.getNumberOfUserCalls();
    }

    /**
     * 获取各个接口的调用次数
     *
     * @return 返回数据
     */
    @Override
    public List<ChartVO> getNumberOfInterfaceCalls() {
        return userInterfaceInfoMapper.getNumberOfInterfaceCalls();
    }
}




